<?php
class ClubCaching
{
    const IDC               = 'club';
    const SHARDING_ID       = 0;
    const CURRENT_ZONE_NAME = ZONE_ENV;
    //const CURRENT_ZONE_NAME = 'hn';
    const HCM_ZONE_NAME     = 'hcm';
    const HN_ZONE_NAME      = 'hn';
    /**
     * Memcache key
     */
    const CLUB_LIST_ARTICLE_CATEGORY    = "club_list_article_category";
    const CLUB_LIST_ARTICLE_HOT         = "club_list_article_hot";
    const CLUB_LIST_ARTICLE_HOT_EXPIRED = "club_list_article_hot_expired";
    const CLUB_ARTICLE_DETAIL           = "club_article_detail";
    const CLUB_ARTICLE_STATISTIC        = "club_article_statistic";
    const PV_CLUB_VALID                 = "club_pageview_valid";
    const PV_CLUB_VALID_EXPIRED         = "club_pageview_valid_expired";
    const CLUB_LIST_TEAM_EXPRESSION     = "club_list_team_expression";
    const CLUB_CHECK_ARTICLE_USER_VOTE  = "club_check_article_user_vote";
    const CLUB_LIST_COMMENT_ARTICLE     = "club_list_comment_article";
    const CLUB_LIST_MY_POST             = "club_list_my_post";

    /**
     * Instance
     */
    protected static $_instance = null;

    /**
     * Constructor of class
     */
    protected function  __construct()
    {

    }

    /**
     * Get singleton instance
     */
    public final static function getInstance()
    {
        //Check instance
        if ( is_null(self::$_instance) )
        {
            self::$_instance    = new self();
        }//end if

        //Return instance
        return self::$_instance;
    }

    /**
     * Get object key by type
     */
    public function getObjKeyByType($type, $params)
    {
        $keyCaching = '';
        switch ( $type )
        {
            case self::PV_CLUB_VALID:
                $keyCaching = $this->makeRedisKey(Fpt_Global::getPrefixCaching(self::PV_CLUB_VALID), $params);
                break;
            case self::PV_CLUB_VALID_EXPIRED:
                $keyCaching = Fpt_Global::getPrefixCaching(self::PV_CLUB_VALID_EXPIRED);
                break;
            case self::CLUB_ARTICLE_STATISTIC:
                $keyCaching = $this->makeRedisKey(Fpt_Global::getPrefixCaching(self::CLUB_ARTICLE_STATISTIC), $params);
                break;
            case self::CLUB_ARTICLE_DETAIL:
                $keyCaching = $this->makeRedisKey(Fpt_Global::getPrefixCaching(self::CLUB_ARTICLE_DETAIL), $params);
                break;
            case self::CLUB_LIST_ARTICLE_HOT:
                $keyCaching = Fpt_Global::getPrefixCaching(self::CLUB_LIST_ARTICLE_HOT);
                break;
            case self::CLUB_LIST_ARTICLE_HOT_EXPIRED:
                $keyCaching = Fpt_Global::getPrefixCaching(self::CLUB_LIST_ARTICLE_HOT_EXPIRED);
                break;
            case self::CLUB_LIST_ARTICLE_CATEGORY:
                $keyCaching = $this->makeRedisKey(Fpt_Global::getPrefixCaching(self::CLUB_LIST_ARTICLE_CATEGORY), $params);
                break;
            case self::CLUB_LIST_TEAM_EXPRESSION:
                $keyCaching = Fpt_Global::getPrefixCaching(self::CLUB_LIST_TEAM_EXPRESSION);
                break;
            case self::CLUB_CHECK_ARTICLE_USER_VOTE:
                $keyCaching = $this->makeRedisKey(Fpt_Global::getPrefixCaching(self::CLUB_CHECK_ARTICLE_USER_VOTE), $params);
                break;
            case self::CLUB_LIST_COMMENT_ARTICLE:
                $keyCaching = $this->makeRedisKey(Fpt_Global::getPrefixCaching(self::CLUB_LIST_COMMENT_ARTICLE), $params);
                break;
            case self::CLUB_LIST_MY_POST:
                $keyCaching = $this->makeRedisKey(Fpt_Global::getPrefixCaching(self::CLUB_LIST_MY_POST), $params);
                break;
            default:
                break;
        }//end switch

        //Return
        return $keyCaching;
    }

    /**
     * Make redis key
     */
    public function makeRedisKey($format, $args)
    {
        if ( !is_array($args) )
        {
            //Return
            return sprintf($format, $args);
        }//end if

        //Merge param
        $format = array($format);
        $args   = array_merge($format, $args);

        //Return
        return call_user_func_array("sprintf", $args);
    }
}